<?php
require '/config.php';
require '/include.php';

$catMenu = catMenu();

// Header Cache infomation
header('Content-Type: text/html; charset=UTF-8');
header('Expires: ' . EXPRIRES);
header('Last-Modified: ' . EXPRIRES);
header("Cache-Control: private, must-revalidate");
header('Cache-Control: max-age=' . MAXAGE);

// Popular Games
$Qry = 'SELECT g_name, g_url, images
		FROM games
		WHERE active = 1 AND popular = 1
		ORDER BY RAND()
		LIMIT ' . index_limit;
$popular = $db->fetchAll($Qry);

$populargames = array();
foreach ($popular as $key=>$value){
	$imgpath = DC_ROOT . '/data/uploads/' . $value['images'];

	if ($value['images'] && file_exists($imgpath))
		$value['images'] = '/data/uploads/' . $value['images'];
	else
		$value['images'] = '/static.jpg';

	$populargames[] = $value;
}

$smarty->assign('populargames', $populargames);

// Top Games
$Qry = 'SELECT g_name, g_url, images
		FROM games
		WHERE active = 1 AND views > 0
		ORDER BY views DESC
		LIMIT ' . index_limit;
$top = $db->fetchAll($Qry);

$topgames = array();
foreach ($top as $key=>$value){
	$imgpath = DC_ROOT . '/data/uploads/' . $value['images'];

	if ($value['images'] && file_exists($imgpath))
		$value['images'] = '/data/uploads/' . $value['images'];
	else
		$value['images'] = '/static.jpg';

	$topgames[] = $value;
}

$smarty->assign('topgames', $topgames);

// Lasted Games
$Qry = 'SELECT g_name, g_url, images
		FROM games
		WHERE active = 1
		ORDER BY g_id DESC
		LIMIT ' . index_limit;
$latest = $db->fetchAll($Qry);

$newgames = array();
foreach ($latest as $key=>$value){
	$imgpath = DC_ROOT . '/data/uploads/' . $value['images'];

	if ($value['images'] && file_exists($imgpath))
		$value['images'] = '/data/uploads/' . $value['images'];
	else
		$value['images'] = '/static.jpg';

	$newgames[] = $value;
}

$smarty->assign('newgames', $newgames);

// SEO
$smarty->assign('is_home', true);
$smarty->assign('itemprop', true);
$smarty->assign('page_title', $lang['page_title']);
$smarty->assign('page_keywords', $lang['page_keywords']);
$smarty->assign('page_description', $lang['page_description']);
$smarty->assign('image_src', BASE_URL . 'static/img/chipgames.jpg');
$smarty->assign('base_url', BASE_URL);

// display theme using smarty
$smarty->display(SKIN . '/index.tpl');